package TenoriOn.managers
{
	import TenoriOn.Grid;
	import TenoriOn.GridData;
	
	import flash.geom.Point;
	
	public class OctaveManager implements IGridManager
	{
		private var _grid:Grid;
		private var _scale:Vector.<Number>=new Vector.<Number>(16);
		
		private var _hasControl:Boolean;
		public function get hasControl():Boolean{
			return _hasControl;
		}
		
		public function OctaveManager()
		{
			calculateScale();
		}
		
		public function get MANAGER_TYPE():String
		{
			return "OctaveManager";
		}
		
		public function makeGridData():GridData
		{
			var level:int=(_octave-2)*4;
			var gData:GridData=new GridData();
			for(var i:int=0;i<16;i++){
				gData.data[i][level]=1;
				gData.data[i][level+1]=1;
				gData.data[i][level+2]=1;
				gData.data[i][level+3]=1;
			}
			return gData;
		}
		
		public function takeControl(grid:Grid):void
		{
			grid.manager=this;
			_hasControl=true;
			_grid=grid;
			updateGrid();
		}
		
		public function releaseControl():void
		{
			_hasControl=false;
		}
		
		public function onGridUpdate(id:Point):void
		{
			_octave=Math.floor(id.y/4)+2;
			calculateScale();
			updateGrid();
		}
		
		private function calculateScale():void{
			var o:int=_octave-4;
			_scale=new <Number>[
				440*Math.pow(2,(-21+o)/12),	//C-1
				440*Math.pow(2,(-19+o)/12),	//D-1
				440*Math.pow(2,(-17+o)/12),	//E-1
				440*Math.pow(2,(-16+o)/12),	//F-1
				440*Math.pow(2,(-14+o)/12),	//G-1
				440*Math.pow(2,(-12+o)/12),	//A-1
				440*Math.pow(2,(-10+o)/12),	//B-1
				440*Math.pow(2,(-9+o)/12),	//C
				440*Math.pow(2,(-7+o)/12),	//D
				440*Math.pow(2,(-5+o)/12),	//E
				440*Math.pow(2,(-4+o)/12),	//F
				440*Math.pow(2,(-2+o)/12),	//G
				440*Math.pow(2,o/12),		//A
				440*Math.pow(2,(2+o)/12),	//B
				440*Math.pow(2,(3+o)/12),	//C+1
				440*Math.pow(2,(5+o)/12)	//D+1
			];
		}
		
		public function updateGrid():void
		{
			_grid.redrawGrid(makeGridData());
			_grid.textDisplay.setText("Octave Manager","OCTAVE:"+_octave.toString());
		}
		
		private var _octave:int=4;
		public function get scale():Vector.<Number>{
			return _scale;
		}
	}
}